耶~~~終於邁入第十天,完成了三分之一,今天要來講文字方塊,普遍常會看到的用法會在輸入號密碼的時候,但其實也可以用在很多地方,像是當成算運算式的工具,今天就會舉這兩個例子喔~
♠♣今天的文章大綱♥♦
| 參數 | 說明 | 
|---|---|
| bg | 邊框背景顏色 | 
| fg | 字型顏色 | 
| bd | 邊框大小,預設為 2px | 
| cursor | 滑鼠形狀設定 | 
| font | 字體 | 
| command | 按按鈕時要用的函數或方法。 | 
| fg | 字型顏色 | 
| highlightcolor | 當按鈕具有焦點時顯示的顏色。 | 
| relief | 按鈕邊框的類型。有 SUNKEN、RAISED、GROOVE 和 RIDGE | 
| justify | 顯示多個文本行時,LEFT 靠左對齊每一行; CENTER 使它們居中;或RIGHT 靠右對齊。 | 
| selectbackground | 被選取字串的背景顏色 | 
| selectborederwidth | 被選取字串的背景框寬度 | 
| selectforeground | 被選取字串的文字顏色 | 
| width | 按鈕寬度 | 
| show | 指定文字框內容顯示哪種字符,預設為 ’ * ’ | 
| state | 文字框狀態,有兩種為只能讀取跟可修改,NORMAL、DISABLE,預設值為NORMAL | 
| textvariable | 文字框的值 | 
| xscrollcommand/yscrollcommand | 滾動條,X為水平的,Y為垂直的 | 
| command | 使用者更改內容時,此行會自動執行 | 
| 方法 | 說明 | 
|---|---|
| delete(first, last=None) | 刪除文本框裡的值,直接指定位置 | 
| get() | 獲得文本框的值 | 
| icursor ( index ) | 邊框大小,預設為 2px | 
| cursor | 滑鼠形狀設定 | 
| font | 字體 | 
| command | 按按鈕時要用的函數或方法。 | 
| fg | 字型顏色 | 
| highlightcolor | 當按鈕具有焦點時顯示的顏色。 | 
| relief | 按鈕邊框的類型。有 SUNKEN、RAISED、GROOVE 和 RIDGE | 
| justify | 顯示多個文本行時,LEFT 靠左對齊每一行; CENTER 使它們居中;或RIGHT 靠右對齊。 | 
| selectbackground | 被選取字串的背景顏色 | 
| selectborederwidth | 被選取字串的背景框寬度 | 
| selectforeground | 被選取字串的文字顏色 | 
| width | 按鈕寬度 | 
| show | 指定文字框內容顯示哪種字符,預設為 ’ * ’ | 
| state | 文字框狀態,有兩種為只能讀取跟可修改,NORMAL、DISABLE,預設值為NORMAL | 
| textvariable | 文字框的值 | 
| xscrollcommand/yscrollcommand | 滾動條,X為水平的,Y為垂直的 | 
| command | 使用者更改內容時,此行會自動執行 | 
import tkinter as tk
root = tk.Tk()
root.geometry("350x400+200+300")
root.title('cuteluluWindow')
root.configure(bg="#7AFEC6")
root.iconbitmap('heart_green.ico')
root.geometry('300x300')
L1=tk.Label(root,text='Account',bg='#DDA0DD',fg="#8B008B",
            font=("Algerian",15,"bold"),padx=9)
L2=tk.Label(root,text='Password',bg='#DDA0DD',fg="#8B008B",
            font=("Algerian",15,"bold"))
L1.grid(row=1)
L2.grid(row=2)
E1=tk.Entry(root)
E2=tk.Entry(root,show="*")#設定用*遮住密碼
E1.grid(row=1,column=1)
E2.grid(row=2,column=1)
root.mainloop()
執行結果⬇⬇⬇
import tkinter as tk
root=tk.Tk()
root.geometry("350x400+200+300")
root.title('cuteluluWindow')
root.configure(bg="#7AFEC6")
root.iconbitmap('heart_green.ico')
root.geometry('300x300')
def Info():
    root1 = tk.Tk()
    root1.title('cutelulu')
    root1.configure(bg="#7AFEC6")
    root1.iconbitmap('heart_green.ico')
    root1.geometry('300x300')
    if En.get()=='cutelulu' and En1.get()=='8888':#用get獲得帳密去判斷能不能登入
      l=tk.Label(root1, text='Login successful!', bg="#7AFEC6",fg='#FFD306',
                 font=("Viner Hand ITC",15,"bold"),anchor='c',width=50,height=10)
      l.pack()
    else:
      l=tk.Label(root1,text='Login Error!', bg="#7AFEC6",fg='#CE0000',
                 font=("Viner Hand ITC",15,"bold"),width=50,height=10,anchor='c')
      l.pack()
label=tk.Label(root,text='Account',bg='#DDA0DD',fg="#8B008B",
            font=("Algerian",15,"bold"),anchor='c')
label.grid(row=0)
En=tk.Entry(root)
En.grid(row=0,column=1)
label1=tk.Label(root,text='Password',bg='#DDA0DD',fg="#8B008B",
            font=("Algerian",15,"bold"),anchor='c')
label1.grid(row=1)
En1=tk.Entry(root,show='*')#隱藏密碼
En1.grid(row=1,column=1)
b=tk.Button(root,text='Exit',anchor='c',width=6,height=1,command=root.quit)#quit可以讓pyhon shell結束
b.grid(row=2,column=0)
b1=tk.Button(root,text='Login',anchor='c',width=6,height=1,command=Info)
b1.grid(row=2,column=1)
root.mainloop()
執行結果⬇⬇⬇
輸入帳號跟密碼,這邊預設帳號:cutelulu,密碼:8888
輸入成功介面
失敗介面
import tkinter as tk
root=tk.Tk()
root.geometry("350x400+200+300")
root.title('cuteluluWindow')
root.configure(bg="#7AFEC6")
root.iconbitmap('heart_green.ico')
root.geometry('300x300')
def math():
    out.configure(text = 'Answer: '+ str(eval(equ.get())))
              
label =tk.Label(root, text="Enter your math question:",bg="#7AFEC6",fg='#FFAAD5',font=("Ravie",10,"bold"))
label.pack()
equ = tk.Entry(root)#輸入要算的東西
equ.pack()
out = tk.Label(root,bg="#7AFEC6",fg='#FF5151',font=("Ravie",15,"bold"))#儲存計算結果
out.pack()
btn = tk.Button(root,text="count",command=math)#執行鈕
btn.pack()
root.mainloop()
執行結果⬇⬇⬇
輸入想要算的運算式
結果會回傳到介面中
以上是今天的Entry()方法,終於到第十天了,算是一個關卡,代表再二十天就可以完成這個任務了,
耶~~~
謝謝大家願意看我的文章,有問題都可以留言討論喔~~~